package com.faner.fast.util;

import cn.hutool.http.HttpUtil;
import cn.hutool.http.useragent.UserAgentUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
/**
 * description: FastUtil
 * date: 2020/11/23 13:27
 * author: faner
 */
@Slf4j
public class FastUtil {

    public static void printSystemUpBanner(Environment environment) {
        String banner = "-----------------------------------------\n" +
                "服务启动成功，时间：" + DateUtils.coverLocalDateTimeToStr(LocalDateTime.now(), DateUtils.FULL_TIME_SPLIT_PATTERN) + "\n" +
                "服务名称：" + environment.getProperty("spring.application.name") + "\n" +
                "端口号：" + environment.getProperty("server.port") + "\n" +
                "-----------------------------------------";
        System.out.println(banner);
    }

    /**
     * 获取浏览器名称
     * @param request
     * @return
     */
    public static String getBrowser(HttpServletRequest request) {
        return UserAgentUtil.parse(request.getHeader("user-agent")).getBrowser().getName();
    }

    /**
     * 根据ip获取详细地址
     */
    public static String getCityInfo(String ip) {
        String api = String.format("http://whois.pconline.com.cn/ipJson.jsp?ip=%s&json=true", ip);
        JSONObject object = JSONUtil.parseObj(HttpUtil.get(api));
        return object.get("addr", String.class);
    }

}
